library(ggplot2)
library(yaml)
library(readr)

CONFIG <- yaml.load_file('config_global.yaml')
build  <- CONFIG$build$descriptive
source(sprintf("%s/library.r", CONFIG$source$lib))

surveys <- read_csv("source/raw/survey_list/survey_list.csv")
surveys <- surveys %>% filter(`Does an affect question exist` == "Yes") %>% mutate(Sample = (`Used` == "Yes"))

# Record the total number of surveys used in the sample
write_gslab_table(sum(surveys$Sample), sprintf("%s/sample_survey_num.txt", build), "<tab:sample_survey_num>")

## Plot
# http://www.sthda.com/english/wiki/ggplot2-dot-plot-quick-start-guide-r-software-and-data-visualization
samp_countries <- c("US", "Switzerland", "France", "Denmark", "Canada", "New Zealand", "Japan", "Australia", "UK (Britain)", "Norway", "Sweden", "Germany")
oth_countries  <- c("Austria", "Belgium", "Finland", "Greece", "Iceland", "Ireland", "Italy", "Luxembourg", "Netherlands", "Portugal", "Spain", "Turkey")
surveys$Country <- factor(surveys$Country, levels = c(rev(oth_countries[order(oth_countries)]), rev(samp_countries[order(samp_countries)]))) # https://rstudio-pubs-static.s3.amazonaws.com/7433_4537ea5073dc4162950abb715f513469.html
surveys$Year    <- factor(surveys$Year, levels = c(1960:2020))

surveys_to_plot <- surveys %>% filter(Country %in% c(samp_countries, oth_countries))
surveys_to_plot <- surveys_to_plot[!(duplicated(surveys_to_plot[, c("Country", "Year")])               & !surveys_to_plot$Sample), ]
surveys_to_plot <- surveys_to_plot[!(duplicated(surveys_to_plot[, c("Country", "Year")], fromLast = T) & !surveys_to_plot$Sample), ]
surveys_to_plot$Year <- as.numeric(as.character(surveys_to_plot$Year))

bold <- c()
for (i in levels(surveys_to_plot$Country)){
  bold <- c(bold, ifelse(i %in% c("US", "Switzerland", "France", "Denmark", "Canada", "New Zealand", "Japan", "Australia", "UK (Britain)", "Norway", "Sweden", "Germany"), "bold", "plain"))
}

p <- ggplot(surveys_to_plot, aes(x = Year, y = Country, colour = Sample)) + xlim(1965, 2020) + geom_point(show.legend = F)+ geom_point(shape = 1, colour = "black", show.legend = F) + # https://stackoverflow.com/questions/10437442/place-a-border-around-points/10437545
            scale_color_manual(values = c("grey65", "black")) + scale_x_continuous(breaks = c(1970, 1980, 1990, 2000, 2010, 2020)) + scale_y_discrete(drop = F) # https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot
p <- p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
               panel.background = element_blank(), axis.line = element_line(colour = "black"),
               axis.text = element_text(size = 12), 
               axis.text.y = element_text(face = bold)) # https://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2 and https://stackoverflow.com/questions/20609716/changing-format-of-some-axis-labels-in-ggplot2-according-to-condition
p <- p + geom_vline(xintercept = 1985, linetype = 'dotted', colour = "grey65", size = .6) 
ggsave(p, filename = sprintf("%s/survey_dotplot.pdf", build), height = 8, width = 8)
